package com.ljd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.entity.EduTeacher;
import com.ljd.entity.vo.TeacherQuery;
import com.ljd.service.EduTeacherService;
import com.ljd.util.R;
import com.why.handler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

import java.util.List;
import java.util.Map;
;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2021-10-20
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    //1 查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public R delete( @ApiParam(name = "id", value = "讲师ID", required = true)
        @PathVariable String id){
        eduTeacherService.removeById(id);
        return R.ok();
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分页讲师管理")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageList(@PathVariable Integer current,
                      @PathVariable Integer limit){

//        try {
//            int i=10/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"出现自定义异常");
//
//        }
      //  int i=10/0;
        Page<EduTeacher> page = new Page<>(current,limit);
        eduTeacherService.page(page,null);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",records);
        map.put("total",total);
        return R.ok().data(map);
    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "分页讲师列表")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Integer current,
                       @PathVariable Integer limit,
                     @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current,limit);
        eduTeacherService.pageQuery(page,teacherQuery);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("rows",records);
        map.put("total",total);

        return R.ok().data("items",map);
    }


    //添加讲师接口的方法
    @ApiOperation(value = "新增讲师")
    @PostMapping("/addTeacher")
    public R save(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save == true){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "根据Id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //讲师修改功能
    @ApiOperation(value = "修改讲师")
    @PostMapping("/updateTeacher")
    public R update(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update == true){
            return R.ok();
        }else {
            return R.error();
        }
    }



}

